package com.example.takeaway.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.takeaway.common.BaseContext;
import com.example.takeaway.common.R;
import com.example.takeaway.entity.Dish;
import com.example.takeaway.entity.ShoppingCart;
import com.example.takeaway.service.DishService;
import com.example.takeaway.service.ShoppingCartService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/shoppingCart")
public class ShoppingCartController {


    private final ShoppingCartService shoppingCartService;
    private  final DishService dishService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */

    @PostMapping("/add")
    public R<ShoppingCart> add(HttpSession httpSession ,  @RequestBody ShoppingCart shoppingCart){

        log.info("购物车数据：--------->{}",shoppingCart.toString());


        //设置用户id --》 指定当前是哪个用户的购物车数据
        Long userId = (Long)httpSession.getAttribute("user");
        shoppingCart.setUserId(userId);

        //查询当前购物车里面的菜品/套餐是否已经加入到了购物车里面
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<ShoppingCart>();
        wrapper.eq(ShoppingCart::getUserId,userId);


        if(dishId != null){

            //添加到购物车的是菜品

            wrapper.eq(ShoppingCart::getDishId , shoppingCart.getDishId());



        } else {

            //添加到都无从多的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());


        }

        ShoppingCart one = shoppingCartService.getOne(wrapper);

        if(one == null){
            //如果存在 则添加到购物车 数量默认是一
            shoppingCart.setNumber(1);

            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;

        }else {
            // 如果存在 在原来数据基础上加1
            Integer number = one.getNumber();
            one.setNumber(number + 1 );
            shoppingCartService.updateById(one);


        }

        return R.success(one);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> SubCart(HttpSession httpSession , @RequestBody ShoppingCart shoppingCart){


        // 构建查询条件 userid 和 dishid/setmealId 进行查询
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId , httpSession.getAttribute("user"));

        //判断是dish 还是 setMeal
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){

            wrapper.eq(ShoppingCart::getDishId , dishId);

        }else {

            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        ShoppingCart one = shoppingCartService.getOne(wrapper);

        Integer number = one.getNumber();

        if(number >= 1 ){

            one.setAmount(BigDecimal.valueOf(number - 1));
            shoppingCartService.updateById(one);
        }else {
            one.setAmount(BigDecimal.valueOf(0));
            shoppingCartService.remove(wrapper);

        }

        return R.success(one);


    }





    /**
     * 查看购物车
     * @return
     */

    @GetMapping("/list")
    public R<List<ShoppingCart>> GetCart(HttpSession httpSession){


        log.info("--------------> 查看购物车");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,httpSession.getAttribute("user"));
        wrapper.orderByAsc(ShoppingCart::getCreateTime);


        List<ShoppingCart> list = shoppingCartService.list(wrapper);

        return R.success(list);

    }

    /**
     * 清空购物车
     * @return
     */

    @DeleteMapping("/clean")
    public R<String> clean(HttpSession httpSession){

        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,httpSession.getAttribute("user"));

        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);

        return R.success("清空成功");


    }

}
